//
//  PywInputView.m
//  PywSdk
//
//  Created by Jade on 4/8/16.
//  Copyright © 2016 zero. All rights reserved.
//

#import "PywInputView.h"
#import "PywMasonry.h"
@interface PywInputView()
@property (nonatomic,strong) UIView *inputLeftView;
@property (nonatomic,strong) UIView *inputRightView;
@property (nonatomic,strong) UIView *lineView;
@end
@implementation PywInputView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUp];
    }
    return self;
}

#pragma mark - setUp
/**
 *  setUp
 */
- (void)setUp
{
    
//    self.backgroundColor = [UIColor colorWithRed:208/255.f green:208/255.f blue:208/255.f alpha:1];
    [self addSubview:self.inputLeftView];
    [self addSubview:self.inputRightView];
    [self addSubview:self.inputTF];
    [self addSubview:self.lineView];
    
    WEAK_SELF;
    [self.inputLeftView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.top.bottom.offset(0);
        make.width.mas_offset(30);
    }];
    [self.inputRightView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.right.top.bottom.offset(0);
        make.width.mas_offset(0);
    }];
    [self.inputTF mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.equalTo(weakSelf.inputLeftView.mas_right);
        make.right.equalTo(weakSelf.inputRightView.mas_left);
        make.top.bottom.equalTo(weakSelf);
    }];
    [self.lineView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.equalTo(weakSelf.mas_bottom);
        make.height.mas_equalTo(1);
    }];
}

- (UIView *)inputRightView
{
    if (!_inputRightView) {
        _inputRightView = [UIView new];
    }
    return _inputRightView;
}

- (UIView *)inputLeftView
{
    if (!_inputLeftView) {
        _inputLeftView = [UIView new];
    }
    return _inputLeftView;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = UIColorFromRGB(0xD2D2D2);
    }
    return _lineView;
}

- (NSString *)getText{
    return self.inputTF.text;
}

- (NSString *)text
{
    return self.inputTF.text;
}

#pragma mark - getter and setter
- (PywInputTF *)inputTF
{
    if (!_inputTF) {
        _inputTF = [[PywInputTF alloc] init];
        _inputTF.backgroundColor = [UIColor whiteColor];
        _inputTF.font = [UIFont systemFontOfSize:14.f];
    }
    return _inputTF;
}

- (void)setRightView:(UIView *)rightView
{
    if (rightView) {
        _rightView = rightView;
//        self.inputTF.rightView = rightView;
        [self.inputRightView addSubview:rightView];
        
        WEAK_SELF;
        [self.inputRightView mas_updateConstraints:^(PywMASConstraintMaker *make) {
            make.width.mas_equalTo(30);
        }];
        [rightView mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerX.centerY.offset(0);
            make.size.mas_equalTo(rightView.frame.size);
        }];
    }
}


- (void)setLeftView:(UIView *)leftView
{
    if (leftView) {
        _leftView = leftView;
//        self.inputTF.leftView = leftView;
        [self.inputLeftView addSubview:leftView];
        WEAK_SELF;
        [leftView mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerX.centerY.equalTo(weakSelf.inputLeftView);
            make.size.mas_equalTo(leftView.frame.size);
        }];
    }
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment{
    _textAlignment = textAlignment;
    self.inputTF.textAlignment = textAlignment;
}

- (void)setRightViewMode:(UITextFieldViewMode)rightViewMode
{
    if (rightViewMode) {
        _rightViewMode = rightViewMode;
        self.inputTF.rightViewMode = rightViewMode;
    }
}

- (void)setDelegate:(id<UITextFieldDelegate>)delegate
{
    if (delegate) {
        _delegate = delegate;
        self.inputTF.delegate = delegate;
    }
}

- (void)setPlaceholder:(NSString *)placeholder
{
    if (placeholder) {
        _placeholder = placeholder;
        self.inputTF.placeholder = placeholder;
    }
}

- (void)setSecureTextEntry:(BOOL)secureTextEntry
{
    if (secureTextEntry) {
        _secureTextEntry = secureTextEntry;
        [self.inputTF setSecureTextEntry:secureTextEntry];
    }
}

- (void)setText:(NSString *)text
{
    if (text) {
//        _text = text;
        self.inputTF.text = text;
    }
}

- (void)setTextColor:(UIColor *)textColor
{
    if (textColor) {
        _textColor = textColor;
        self.inputTF.textColor = textColor;
    }
}

- (void)setFont:(UIFont *)font
{
    if (font) {
        _font = font;
        self.inputTF.font = font;
    }
}

#pragma mark - Action Method
/**
 *  bing method to TextFiled
 *
 *  @param target        <#target description#>
 *  @param action        <#action description#>
 *  @param controlEvents <#controlEvents description#>
 */
- (void)addTarget:(nullable id)target
           action:(SEL)action
 forControlEvents:(UIControlEvents)controlEvents
{
    [self.inputTF addTarget:target
                 action:action
       forControlEvents:controlEvents];
}
@end
